package com.tjtt.tdm.base;

import java.lang.reflect.Method;
import java.sql.Clob;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.tjtt.common.utils.StringUtil;
import com.tjtt.common.utils.TotalFlowXmlTool;
import com.tjtt.tdm.exception.DaoException;
import com.tjtt.tdm.flow.bean.FlowDetailed;
import com.tjtt.tdm.flow.bean.FlowTask;
import com.tjtt.tdm.flow.bean.FlowTaskInfo;
import com.tjtt.tdm.flow.bean.Flowstate;
import com.tjtt.tdm.flow.bean.Flowtype;
import com.tjtt.tdm.flow.bean.StepEntity;
import com.tjtt.tdm.flow.service.IFlowformService;
import com.tjtt.tdm.flow.service.IFlowtypeService;
import com.tjtt.tdm.system.bean.LogInfo;
import com.tjtt.tdm.system.bean.Users;
import com.tjtt.tdm.system.service.ConfigService;


public class BaseAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected static JsonConfig cfg;

	private String page;
	private String rows;
	private String sidx;
	private String sord;
	
	private long start = 0;
	private long limit = 0;
	private String sort;
	private String dir;
	
	private String stateid;
	private String flowid;
	private String taskid;
	private String mainid;
	private String steptcontent;
	//none表示不显示，block表示显示
	private String isshowstept; 
	
	
	/**
	 * 获取系统统一序列ID
	 * @return
	 */
	public String getID(){
		try {
			WebApplicationContext factory = WebApplicationContextUtils.getRequiredWebApplicationContext(getRequest().getSession().getServletContext());  
			ConfigService configService =  (ConfigService)factory.getBean("configService");
			return configService.getSeqTdm();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 获取登录用户ID
	 * String
	 * 2011-5-10下午04:38:05
	 * author：李数
	 */
	public String getUserId(){
		String userid = ServletActionContext.getRequest().getSession().getAttribute("TDM_USER_ID").toString();
		return userid;
	}
	public String getOrgIds(){
		String orgid = ServletActionContext.getRequest().getSession().getAttribute("ORG_ID").toString();
		return orgid;
	}
	public String getOrgName(){
		String orgname = ServletActionContext.getRequest().getSession().getAttribute("ORG_NAME").toString();
		return orgname;
	}
	/*
	 * 取得用户的登陆名
	 */
	public String getUserName(){
		String username = ServletActionContext.getRequest().getSession().getAttribute("loginname").toString();
		return username;
	}
	/*
	 * 取得用的中文名
	 */
	public String getUserNM(){
		String username = ServletActionContext.getRequest().getSession().getAttribute("username").toString();
		return username;
	}
	
	/**
	 * 获取角色字符串 格式"123,23,43,435"
	 * @return String
	 * @author 李数
	 * @date 2012-10-22 下午06:14:42
	 */
	public String getRole(){
		String role = ServletActionContext.getRequest().getSession().getAttribute("ROLE_INFO").toString();
		return role;
	}
	/**
	 * 获取部门ID
	 * @return
	 */
	public String getDeptid(){
		String dept = ServletActionContext.getRequest().getSession().getAttribute("USER_DEPT").toString();
		return dept;
	}
	/**
	 * 获取部门name
	 * @return
	 */
	public String getDeptName(){
		String deptName = ServletActionContext.getRequest().getSession().getAttribute("USER_DEPT_NAME").toString();
		return deptName;
	}
	/**
	 * 获取设备到期提醒天数
	 * @return
	 */
	public String getSysEquipmentDay(){
		String dept = ServletActionContext.getRequest().getSession().getAttribute("SYS_EQUIPMENT_DAY").toString();
		return dept;
	}
	/**
	 * 从Session中获取用户信息
	 * @return
	 */
	public Users getUser(){
		return (Users) getSessionAttValue("TDM_USER");
	}
	/**
	 * 添加处理结果信息
	 * void
	 * 2011-5-10下午04:38:36
	 * author：李数
	 */
	public void addMessage(String message){
		ServletActionContext.getRequest().setAttribute("message", message);
	}
	/**
	 * 取得日志的基本信息方法，包括：当前请求的ip、请求的时间、登录事件、用户id、用户名称、组织id、组织name
	*/
	public LogInfo getLogBase(){
		LogInfo logInfo = new LogInfo();
		
		logInfo.setLogCreatTime(new Timestamp(System.currentTimeMillis()));
		String ip = this.getRequest().getRemoteAddr();
		if("0:0:0:0:0:0:0:1".equals(ip)){
			ip = "127.0.0.1";
		}
		logInfo.setLogLoginIp(ip);
		Object loginDate = this.getSessionAttValue("loginTime");
		if(null != loginDate){
			logInfo.setLogLoginTime(new Timestamp(System.currentTimeMillis()));
		}
		Users user = this.getUser();
		if(null != user){
			logInfo.setLogOrgId(this.getUser().getOrganization().getOrgId());
			logInfo.setLogOrgName(this.getUser().getOrganization().getOrgName());
			logInfo.setLogUserId(this.getUser().getUserid());
			logInfo.setLogUserName(this.getUser().getUsername());
		}	
		
		return logInfo;
	}
	/*
	 * 取得页面传过来的列表参数
	 */
	public Map getPagerMap(){
		if(page==null) page ="1";
		if(sidx==null) sidx ="id";
		if(rows==null) rows ="10";
		if(sord==null) sord ="asc";
		Map filterMap = new HashMap();//存储参数的map
		filterMap.put("page", Integer.parseInt(page));
		filterMap.put("rows", Integer.parseInt(rows));
		filterMap.put("sidx", sidx);
		filterMap.put("sord", sord);
		return filterMap;
	}
	
	/**
	 * 得到往页面传的josn对象
	 * String
	 * 2011-4-26下午09:06:10
	 * author：李数
	 */
	public String getPagerJson(PageResult result,String[] viewArray){
		if(rows==null)rows="10";
		int rowsInt=Integer.parseInt(rows);
		int totalRows = result.getRowCount(); //所有的行数
		int totalPages = (totalRows + rowsInt - 1) / rowsInt; //总页数
		List rowAll = result.getData();   //查询出的所有记录
		JSONObject obj = new JSONObject();
		obj.put("page", ""+page);
		obj.put("total",totalPages);
		obj.put("records",""+totalRows);
		JSONArray lineitemArray = new JSONArray(); 
		Iterator it = rowAll.iterator();
		while (it.hasNext()) { 
			lineitemArray.add(getPropertyValue(it.next(),viewArray));
		} 
		obj.put("rows", lineitemArray);
		return obj.toString();
	}
	
	/**
	 * 通过反射机制把查询结果存到json对象里
	 * JSONObject
	 * 2011-4-26下午09:57:54
	 * author：李数
	 */
	public JSONObject getPropertyValue(Object obj,String[] listArray){ 
		try {
			JSONObject objlineitem = new JSONObject();	
			// 获得对象的类型
	        Class<?> classType = obj.getClass(); 
	        for (String fieldName:listArray)
	        { 
	        	if(fieldName.indexOf(":")>0){
	        		if(fieldName.indexOf(".")>0){
	        			String rrealfieldName = fieldName.substring(0,fieldName.indexOf(":"));
		        		String jjsonStr = fieldName.substring(fieldName.indexOf(":")+1);
		        		
		        		String realfieldName = rrealfieldName.substring(0,rrealfieldName.indexOf("."));
		        		String jsonStr = rrealfieldName.substring(rrealfieldName.indexOf(".")+1); 
		        		JSONArray json = JSONArray.fromObject(jjsonStr);
		        		JSONObject job = json.getJSONObject(0);
		        		String getMethodName = "get" + realfieldName.substring(0, 1).toUpperCase() + realfieldName.substring(1);
		        		// 获得和属性对应的getXXX()方法
		 	            Method getMethod = classType.getMethod(getMethodName, new Class[] {});
		 	            // 调用原对象的getXXX()方法
		 	            Object newobj = getMethod.invoke(obj, new Object[] {});
		 	           if(newobj !=null){
			 	            // 获得对象的类型
			 		        Class<?> newclassType = newobj.getClass(); 
			 		        // 获得和属性对应的getXXX()方法的名字
			 	            String getMethodNamenew = "get" + jsonStr.substring(0, 1).toUpperCase() + jsonStr.substring(1);
			 	            // 获得和属性对应的getXXX()方法
			 	            Method getMethodnew = newclassType.getMethod(getMethodNamenew, new Class[] {});
			 	            Object value = getMethodnew.invoke(newobj, new Object[] {});
			 	            if(value!=null && value !=""){
			 	            	if(job.containsKey(value.toString())){
			 	            		 String realvalue = job.getString(value.toString());
				 	            	 if(realvalue!=null){
				 	            		objlineitem.put(rrealfieldName, realvalue);
				 	            	 }
			 	            	}else{
		    	        			 objlineitem.put(rrealfieldName, value.toString());
		    	        		}
			 	            } 
		 		        }
	        		}else{
	        			String realfieldName = fieldName.substring(0,fieldName.indexOf(":"));
		        		String jsonStr = fieldName.substring(fieldName.indexOf(":")+1);
		        		JSONArray json = JSONArray.fromObject(jsonStr);
		        		JSONObject job = json.getJSONObject(0);
		        		String firstLetter = realfieldName.substring(0, 1).toUpperCase();
		 	            // 获得和属性对应的getXXX()方法的名字
		 	            String getMethodName = "get" + firstLetter + realfieldName.substring(1);
		 	            // 获得和属性对应的getXXX()方法
		 	            Method getMethod = classType.getMethod(getMethodName, new Class[] {});
		 	            // 调用原对象的getXXX()方法
		 	            Object value = getMethod.invoke(obj, new Object[] {});
		 	            if(value !=null && value !=""){
		 	            	if(job.containsKey(value.toString())){
		 	            		 String realvalue = job.getString(value.toString());
			 	            	 if(realvalue!=null){
			 	            		objlineitem.put(realfieldName, realvalue);
			 	            	 }
		 	            	}else{
	    	        			 objlineitem.put(realfieldName, value.toString());
	    	        		}
		 	            }
	        		}
	        	}else if(fieldName.indexOf(".")>0){ //如果是关联对象
	        		String realfieldName = fieldName.substring(0,fieldName.indexOf("."));
	        		String jsonStr = fieldName.substring(fieldName.indexOf(".")+1); 
	        		String getMethodName = "get" + realfieldName.substring(0, 1).toUpperCase() + realfieldName.substring(1);
	        		// 获得和属性对应的getXXX()方法
	 	            Method getMethod = classType.getMethod(getMethodName, new Class[] {});
	 	            // 调用原对象的getXXX()方法
	 	            Object newobj = getMethod.invoke(obj, new Object[] {});
	 	           if(newobj !=null){
		 	            // 获得对象的类型
		 		        Class<?> newclassType = newobj.getClass(); 
		 		        // 获得和属性对应的getXXX()方法的名字
		 	            String getMethodNamenew = "get" + jsonStr.substring(0, 1).toUpperCase() + jsonStr.substring(1);
		 	            // 获得和属性对应的getXXX()方法
		 	            Method getMethodnew = newclassType.getMethod(getMethodNamenew, new Class[] {});
		 	            Object value = getMethodnew.invoke(newobj, new Object[] {});
		 	            if(value!=null){
		 	            	 objlineitem.put(fieldName, value.toString());
		 	            } 
	 		        }
	        	}else{
	        		String firstLetter = fieldName.substring(0, 1).toUpperCase();
	 	            // 获得和属性对应的getXXX()方法的名字
	 	            String getMethodName = "get" + firstLetter + fieldName.substring(1);
	 	            // 获得和属性对应的getXXX()方法
	 	            Method getMethod = classType.getMethod(getMethodName, new Class[] {});
	 	            // 调用原对象的getXXX()方法
	 	            Object value = getMethod.invoke(obj, new Object[] {});
	 	            if(value!=null){
	 	            	 objlineitem.put(fieldName, value.toString());
	 	            }
	        	}
	        }
			return objlineitem;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 根据PageResult结果返回json
	 * @param result
	 * @return json String
	 */
	public String  getResultJson(PageResult result) {
		int rowsInt=Integer.parseInt(rows);
		int totalRows = result.getRowCount(); //所有的行数
		int totalPages = (totalRows + rowsInt - 1) / rowsInt; //总页数
		JSONObject obj = new JSONObject();
		obj.put("page", ""+page);
		obj.put("total",totalPages);
		obj.put("records",""+totalRows);
		
		String returnStr = JSONSerializer.toJSON(result, cfg).toString();
		returnStr =returnStr.substring(returnStr.indexOf("[")) ;
		returnStr =returnStr.substring(0,returnStr.indexOf("]")+1);
		if(returnStr.indexOf("bookindate")>0&&returnStr.indexOf("itemnamee")>0){
			String s = "";
			String str[] = returnStr.split("}");
			for (int i = 0; i < str.length; i++) {
				if(str[i].indexOf("bookindate")>0&&str[i].indexOf("groupname")>0){
					String string1[] = str[i].split("bookindate");
					String string2[] = string1[1].split("groupname");
					s += string1[0]+"bookindate"+string2[0].split(" ")[0]+"\",\"itemnamee"+string2[1]+"}";
				}else {
					s += str[i];
				}
			}
			returnStr = s;
		}
		returnStr = returnStr.replace(",", "\",\"");
		returnStr = returnStr.replace(":", "\":\"");
		returnStr = returnStr.replace("}\",\"{", "\"},{\"");
		returnStr = returnStr.replace("}]", "\"}]");
		returnStr = returnStr.replace("\"\"", "\"");

		obj.put("rows", returnStr);
		return obj.toString();
	}
	
	/**
	 * 根据PageResult结果返回json
	 * @param result
	 * @return json String
	 */
	public String  getResultJsonSecend(PageResult result) {
		int rowsInt=10;
		int totalRows = result.getRowCount(); //所有的行数
		int totalPages = (totalRows / rowsInt)+1; //总页数
		
		String returnStr = JSONSerializer.toJSON(result, cfg).toString();
		returnStr =returnStr.substring(returnStr.indexOf("[")) ;
		returnStr =returnStr.substring(0,returnStr.indexOf("]")+1);
		returnStr = returnStr.replace(",", "\",\"");
		returnStr = returnStr.replace(":", "\":\"");
		returnStr = returnStr.replace("}\",\"{", "\"},{\"");
		returnStr = returnStr.replace("}]", "\"}]");
		returnStr = returnStr.replace("\"\"", "\"");
		returnStr = returnStr.replace("null", "");
		String string = "{\"page\":\""+page+"\",\"total\":"+totalPages+",\"records\":\""+totalRows+"\",\"rows\":"+returnStr+"}";
		return string;
	}
	
	/**
	 * 得到往页面传的josn对象
	 * String
	 * 2011-4-26下午09:06:10
	 * author：李数
	 */
	public String getColumnJson(PageResult result,String[] viewArray){
		int rowsInt=Integer.parseInt(rows);
		int totalRows = result.getRowCount(); //所有的行数
		int totalPages = (totalRows + rowsInt - 1) / rowsInt; //总页数
		List rowAll = result.getData();   //查询出的所有记录
		JSONObject obj = new JSONObject();
		obj.put("page", ""+page);
		obj.put("total",totalPages);
		obj.put("records",""+totalRows);
		JSONArray lineitemArray = new JSONArray(); 
		Iterator it = rowAll.iterator();
		while (it.hasNext()) { 
			Object[] objlist = (Object[]) it.next();
			lineitemArray.add(getColumnValue(objlist,viewArray));
		} 
		obj.put("rows", lineitemArray);
		return obj.toString();
	}
	
	public JSONObject getColumnValue(Object[] objlist,String[] viewArray) {
		JSONObject objlineitem = new JSONObject();	
        for (int i=0;i<viewArray.length;i++)
        { 
        	if(!StringUtil.isEmpty(viewArray[i])){
        		if(objlist[i] != null ){
        			if(viewArray[i].indexOf(":")>0){
            			String realfieldName = viewArray[i].substring(0,viewArray[i].indexOf(":"));
            			String jsonStr = viewArray[i].substring(viewArray[i].indexOf(":")+1);
    	        		JSONArray json = JSONArray.fromObject(jsonStr);
    	        		JSONObject job = json.getJSONObject(0);
    	        		if(job.containsKey(objlist[i].toString())){
    	        			 String realvalue = job.getString(objlist[i].toString());
    		        		 if(realvalue!=null){
    		        			 objlineitem.put(realfieldName, realvalue.toString());
    		        		 }
    	        		}else{
    	        			 objlineitem.put(realfieldName, objlist[i].toString());
    	        		}
            		}else{
            			 if(objlist[i]!=null){
            				 objlineitem.put(viewArray[i], objlist[i].toString());
            			 } else {
            				 objlineitem.put(viewArray[i], "");
            				}
            		}
	        	}else {
					 objlineitem.put(viewArray[i], "");
				}
        	}
        }
		return objlineitem;
	}
	
	/**
	 * 得到往页面传的josnXml版本对象
	 * String
	 * 2011-4-26下午09:06:10
	 * author：李数
	 */
	public String getColumnXmlJson(PageResult result,String[] viewArray){
		int rowsInt=Integer.parseInt(rows);
		int totalRows = result.getRowCount(); //所有的行数
		int totalPages = (totalRows + rowsInt - 1) / rowsInt; //总页数
		List rowAll = result.getData();   //查询出的所有记录
		JSONObject obj = new JSONObject();
		obj.put("page", ""+page);
		obj.put("total",totalPages);
		obj.put("records",""+totalRows);
		JSONArray lineitemArray = new JSONArray(); 
		Iterator it = rowAll.iterator();
		while (it.hasNext()) { 
			Object[] objlist = (Object[]) it.next();
			lineitemArray.add(getColumnXmlValue(objlist,viewArray));
		} 
		obj.put("rows", lineitemArray);
		return obj.toString();
	}
	
	
	public JSONObject getColumnXmlValue(Object[] objlist,String[] viewArray) {
		JSONObject objlineitem = new JSONObject();	
		objlineitem.put(viewArray[0], objlist[0]);
		Clob  clob = (Clob) objlist[1];
	    String xml = ConvertBLOBtoString(clob);
	    try{
	    Document document = DocumentHelper.parseText(xml);
	    for(int i = 1;i<viewArray.length;i++)
	    {
	    	String columnname = viewArray[i].replace("'", "");
	    	Node node = document.selectSingleNode("//datas/data/"+columnname);
	    	String value = node.getText();
	    	objlineitem.put(columnname,value);
	    }
	    }
	    catch(Exception exp)
	    {
	    	
	    }
		return objlineitem;
	}
	
	public static String ConvertBLOBtoString(Clob clob){
		  String newStr = ""; // 返回字符串
		  try {
			  newStr = clob.getSubString((long)1,(int)clob.length());
		  } catch (Exception exp) {
		   // TODO Auto-generated catch block
			  exp.printStackTrace();
		  } 
		  return newStr;
	}
	
	public BaseAction() {
		 
	}

	/**
	 * 保存request里请求的参数
	 * void
	 * 2011-3-21上午11:18:53
	 * author：李数
	 */
	public JSONObject savePager(){
		JSONObject obj = new JSONObject();
		String page = getRequest().getParameter("page");
		String rows = getRequest().getParameter("rows");
		String sidx = getRequest().getParameter("sidx");
		String sord = getRequest().getParameter("sord");
		if(page==null) page ="1";
		if(sidx==null) sidx ="id";
		if(rows==null) rows ="10";
		if(sord==null) sord ="asc"; 
		return obj;
	}
	
	/**
	 * 通过反射机制把查询结果存到json对象里
	 * @return JSONObject
	 * @date 2011-11-12 Am 09:57:54
	 * @author：zhaoshuyi
	 */
	public JSONObject getObjValueToJSON(Object obj,String[] listArray){ 
		try {
			JSONObject objlineitem = new JSONObject();	
			// 获得对象的类型
	        Class<?> classType = obj.getClass(); 
	        for (String fieldName:listArray)
	        { 
	        	if(!StringUtil.isEmpty(fieldName)){
	        		if(fieldName.indexOf(":")>0){
		        		String realfieldName = fieldName.substring(0,fieldName.indexOf(":"));
		        		String jsonStr = fieldName.substring(fieldName.indexOf(":")+1);
		        		JSONArray json = JSONArray.fromObject(jsonStr);
		        		JSONObject job = json.getJSONObject(0);
		        		String firstLetter = realfieldName.substring(0, 1).toUpperCase();
		 	            // 获得和属性对应的getXXX()方法的名字
		 	            String getMethodName = "get" + firstLetter + realfieldName.substring(1);
		 	            // 获得和属性对应的getXXX()方法
		 	            Method getMethod = classType.getMethod(getMethodName, new Class[] {});
		 	            // 调用原对象的getXXX()方法
		 	            Object value = getMethod.invoke(obj, new Object[] {});
		 	            if(value !=null && value !=""){
		 	            	if(job.containsKey(value.toString())){
		 	            		 String realvalue = job.getString(value.toString());
			 	            	 if(realvalue!=null){
			 	            		objlineitem.put(realfieldName, realvalue);
			 	            	 }else {
			 	            		objlineitem.put(realfieldName, "");
								}
		 	            	}
		 	            }
		        	}else if(fieldName.indexOf(".")>0){ //如果是关联对象
		        		String realfieldName = fieldName.substring(0,fieldName.indexOf("."));
		        		String jsonStr = fieldName.substring(fieldName.indexOf(".")+1); 
		        		String getMethodName = "get" + realfieldName.substring(0, 1).toUpperCase() + realfieldName.substring(1);
		        		// 获得和属性对应的getXXX()方法
		 	            Method getMethod = classType.getMethod(getMethodName, new Class[] {});
		 	            // 调用原对象的getXXX()方法
		 	            Object newobj = getMethod.invoke(obj, new Object[] {});
		 	           if(newobj !=null){
			 	            // 获得对象的类型
			 		        Class<?> newclassType = newobj.getClass(); 
			 		        // 获得和属性对应的getXXX()方法的名字
			 	            String getMethodNamenew = "get" + jsonStr.substring(0, 1).toUpperCase() + jsonStr.substring(1);
			 	            // 获得和属性对应的getXXX()方法
			 	            Method getMethodnew = newclassType.getMethod(getMethodNamenew, new Class[] {});
			 	            Object value = getMethodnew.invoke(newobj, new Object[] {});
			 	            if(value!=null){
			 	            	 objlineitem.put(fieldName, value.toString());
			 	            } else {
		 	            		objlineitem.put(fieldName, "");
							}
		 		        }
		        	}else {
		        		String firstLetter = fieldName.substring(0, 1).toUpperCase();
		 	            // 获得和属性对应的getXXX()方法的名字
		 	            String getMethodName = "get" + firstLetter + fieldName.substring(1);
		 	            // 获得和属性对应的getXXX()方法
		 	            Method getMethod = classType.getMethod(getMethodName, new Class[] {});
		 	            // 调用原对象的getXXX()方法
		 	            Object value = getMethod.invoke(obj, new Object[] {});
		 	            if(value!=null){
		 	            	 objlineitem.put(fieldName, value.toString());
		 	            } else {
	 	            		objlineitem.put(fieldName, "");
						}
		        	}
	        	}
	        }
			return objlineitem;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public IFlowtypeService getFlowtypeService(){
		WebApplicationContext factory = WebApplicationContextUtils.getRequiredWebApplicationContext(getSession().getServletContext());
		IFlowtypeService flowtypeService =  (IFlowtypeService)factory.getBean("flowtypeService");
		return flowtypeService;
	}
 
	
	public IFlowformService getFlowformentityService(){
		WebApplicationContext factory = WebApplicationContextUtils.getRequiredWebApplicationContext(getSession().getServletContext());
		IFlowformService flowformService =  (IFlowformService)factory.getBean("flowformService");
		return flowformService;
	}
	
	public IBaseLogicNode getBaseLogicNode(String nodeinfo){
		WebApplicationContext factory = WebApplicationContextUtils.getRequiredWebApplicationContext(getSession().getServletContext());
		IBaseLogicNode baseLogicNode =  (IBaseLogicNode)factory.getBean(nodeinfo);
		return baseLogicNode;
	}
	
	/*
	 * 初始化发起页面
	 */
	public void initFlowaddBase(){
		String flowid = this.getRequest().getParameter("flowid");
		//设置流程类型id
		this.setFlowid(flowid);
		IFlowtypeService flowtypeService =  getFlowtypeService();

		try {
			Flowtype flowtype = flowtypeService.GetFlowtypeById(Long.parseLong(flowid));
			String filename = flowtype.getFilename();
			String path = PubMethod.GetFlowFilePath()+filename;// 流程对应的.xml 文件
			TotalFlowXmlTool fxs = new TotalFlowXmlTool(path);
			String stateid = fxs.GetFlowBeginState().getStateid();
			Flowstate flowstate = fxs.GetFlowStateByID(stateid); //
			//设置当前状态
			List<StepEntity> steps = fxs.GetListByText(stateid); // 有几个节点。
			//设置是否显示节点步奏和节点步骤的内容
			StringBuffer sbstept = new StringBuffer();
			String isshowstept = "";
			if (steps.size()<=1){
				//设置是否显示节点步奏
				isshowstept = "none";
				StepEntity entity = (StepEntity) steps.get(0);
				sbstept.append("<input type='hidden' name='steptype' id='steptype0' value='"+entity.getId()+"' >");
			}
			else{
				isshowstept = "block";
				int i = 0;
				for(Iterator iter = steps.iterator();iter.hasNext();)
				{
					StepEntity entity = (StepEntity)iter.next();
					if (i==0){
						sbstept.append("<input type='radio' name='steptype' id='steptype"+i+"' checked style='border:0px;padding: 5px;width: 50px;margin-bottom: 5px;color: #666;background: url(../images/input_bg.gif) repeat-x top;' value='"+entity.getId()+"'/>"+entity.getText()+"&nbsp;&nbsp;&nbsp;&nbsp;") ;
					}
					else{
						sbstept.append("<input type='radio' name='steptype' id='steptype"+i+"'  style='border:0px;padding: 5px;width: 50px;margin-bottom: 5px;color: #666;background: url(../images/input_bg.gif) repeat-x top;' value='"+entity.getId()+"'/>"+entity.getText()+"&nbsp;&nbsp;&nbsp;&nbsp;");
					}
					i++;
				}
			}
			getRequest().setAttribute("flowid", flowid);
			getRequest().setAttribute("stateid", flowstate.getStateid());
			getRequest().setAttribute("isshowstept", isshowstept);
			getRequest().setAttribute("steptcontent", sbstept.toString());// 页面中显示流程路径。
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

	/*
	 * 保存发起数据
	 */
	 public String saveFaqiBase(String tablename,String mainid){
			try{
				String userId = ServletActionContext.getRequest().getSession().getAttribute("TDM_USER_ID").toString();
				String userNm = ServletActionContext.getRequest().getSession().getAttribute("username").toString();
				String stateid =getRequest().getParameter("stateid");
				String flowid = getRequest().getParameter("flowid");
				String steptype = getRequest().getParameter("steptype"); // 表单控件
				String flowname = getRequest().getParameter("flowname");
				String nextUserIds = getRequest().getParameter("nextUserIds"); 
				String nextUserNames = getRequest().getParameter("nextUserNames"); 
				IFlowtypeService flowtypeService =  getFlowtypeService();
				IFlowformService flowformService =  getFlowformentityService();
				Flowtype flowtype = flowtypeService.GetFlowtypeById(Long.parseLong(flowid));
				String filename = flowtype.getFilename();
				String path = PubMethod.GetFlowFilePath()+filename;
				TotalFlowXmlTool fxs = new TotalFlowXmlTool(path);
				Flowstate flowstate = fxs.GetNextState(steptype);
				Flowstate beforeflowstate = fxs.GetFlowStateByID(stateid);
				FlowTask flowTask = new FlowTask();
				flowTask.setAddUserId(Long.parseLong(userId));
				flowTask.setAddUserName(userNm);
				flowTask.setFlowName(flowname); // 保存到了flow_task 表中了
				flowTask.setFlowTypeId(Long.parseLong(flowid));
				flowTask.setState(0l);
				flowTask.setFlowStateName(flowstate.getState());
				flowTask.setFlowStateId(flowstate.getStateid());
				flowTask.setTablename(tablename);
				if (!mainid.equals("")){
					flowTask.setMainId(Long.parseLong(mainid));
				}
				if (!flowstate.getRoleIds().equals(""))
				{
					flowTask.setNextRolesId(","+flowstate.getRoleIds()+",");
				}else{
					flowTask.setNextRolesId("");
				}
				flowTask.setNextRolesName(flowstate.getRoleNames());
				flowTask.setNextUsersId(nextUserIds);
				flowTask.setNextUsersName(nextUserNames);
				flowTask.setNextDept(flowstate.getDept());
				flowTask.setNextDeptid(flowstate.getDeptid());
				flowTask.setAddTime(new Timestamp(System.currentTimeMillis()));
				//flowTask.setFaqirenid(Long.parseLong(userId));
				String flowtaskid = flowformService.saveFlowTask(flowTask); // 存flow_task 表
				FlowTaskInfo flowtaskinfo = new FlowTaskInfo();
				flowtaskinfo.setFlowtaskid(Long.parseLong(flowtaskid));
				flowtaskinfo.setFlowStateId(flowstate.getStateid());
				flowtaskinfo.setFlowStateName(flowstate.getState());
				flowtaskinfo.setNextDept(flowstate.getDept());
				flowtaskinfo.setNextDeptid(flowstate.getDeptid());
				if (!flowstate.getRoleIds().equals(""))
				{
					flowtaskinfo.setNextRolesId(","+flowstate.getRoleIds()+",");
				}else{
					flowtaskinfo.setNextRolesId("");
				}
				flowtaskinfo.setNextUsersId(nextUserIds);
				flowtaskinfo.setNextUsersName(nextUserNames);
				flowtaskinfo.setState(0l);
				flowformService.saveFlowTaskInfo(flowtaskinfo);  /// 存 flow_taskinfo 表
				
				FlowDetailed flowdetail = new FlowDetailed();
				flowdetail.setFlowTaskId(Long.parseLong(flowtaskid));
				flowdetail.setFlowTypeId(flowTask.getFlowTypeId());
				flowdetail.setOperatorperson(userNm);
				flowdetail.setOperatorpersonId(Long.parseLong(userId));
				flowdetail.setStatusbeforeid(beforeflowstate.getStateid());
				flowdetail.setStatusbefore(beforeflowstate.getState());
				flowdetail.setStatusafterid(flowstate.getStateid());
				flowdetail.setStatusafter(flowstate.getState());
				flowdetail.setDeptid(Long.parseLong(this.getDeptid()));
				flowdetail.setOperatortime(new Timestamp(System.currentTimeMillis()));
				flowdetail.setActiontype("顺序");
				flowformService.saveFlowDetailed(flowdetail); // 存 flow_detailed
				return "success";
			}catch (Exception e) {
				e.printStackTrace();
				return "error";
			}
	 }

	 //通过判断类型流程节点类型选择不同的
	 public void initFlowupdateCommon(){
		try {
			 String flowid = this.getRequest().getParameter("flowid");
			 IFlowtypeService flowtypeService =  getFlowtypeService();
			 Flowtype flowtype;
			 flowtype = flowtypeService.GetFlowtypeById(Long.parseLong(flowid));
			 String filename = flowtype.getFilename();
			 String path = PubMethod.GetFlowFilePath()+filename;
			 TotalFlowXmlTool fxs = new TotalFlowXmlTool(path);
			 Flowstate flowstate = fxs.GetFlowStateByID(stateid);
			 if (flowstate.getNodetype().equals("0014")){
				 initFlowupdateHuiqian();
			 }
			 else if (flowstate.getNodetype().equals("0016")){
				 initFlowupdateFenzhi();
			 }else{
				 initFlowupdateBase();
			 }
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
	 
	 //通过判断类型流程节点类型选择不同的
	 public String saveUpdateCommon(String tablename,String mainid){
		try {
			String flowid = this.getRequest().getParameter("flowid");
			 IFlowtypeService flowtypeService =  getFlowtypeService();
			 Flowtype flowtype;
			 flowtype = flowtypeService.GetFlowtypeById(Long.parseLong(flowid));
			 String filename = flowtype.getFilename();
			 String path = PubMethod.GetFlowFilePath()+filename;
			 TotalFlowXmlTool fxs = new TotalFlowXmlTool(path);
			 Flowstate flowstate = fxs.GetFlowStateByID(stateid);
			 if (flowstate.getNodetype().equals("0014")){
				 return saveUpdateHuiqianstart(tablename, mainid);
			 }
			 else if (flowstate.getNodetype().equals("0015")){
				 return saveUpdateHuiqianend(tablename, mainid);
			 }
			 else if (flowstate.getNodetype().equals("0016")){
				 return saveUpdateFenzhistart(tablename, mainid);
			 }
			 else if (flowstate.getNodetype().equals("0017")){
				 return saveUpdateFenzhiend(tablename, mainid);
			 }else{
				 return saveUpdateBase(tablename, mainid);
			 }
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return "";
		} catch (DaoException e) {
			e.printStackTrace();
			return "";
		}
	 }
	 
	 /*
	  * 一般更新初始化方法
	  */
	 public void initFlowupdateBase(){
		 //判断节点类型进行处理
		try {
			String flowid = this.getRequest().getParameter("flowid");
			String taskinfoid = this.getRequest().getParameter("taskinfoid");
			//设置流程类型id
			this.setFlowid(flowid);
			IFlowtypeService flowtypeService =  getFlowtypeService();
			IFlowformService flowformService =  getFlowformentityService();
			FlowTaskInfo flowtaskinfo = flowformService.getFlowTaskInfoById(taskinfoid);
			FlowTask flowtask = flowformService.getFlowTaskById(flowtaskinfo.getFlowtaskid().toString());
			Flowtype flowtype = flowtypeService.GetFlowtypeById(Long.parseLong(flowid));
			String filename = flowtype.getFilename();
			String path = PubMethod.GetFlowFilePath()+filename;
			TotalFlowXmlTool fxs = new TotalFlowXmlTool(path);
			Flowstate flowstate = fxs.GetFlowStateByID(stateid);
			//设置当前状态
			List<StepEntity> steps = fxs.GetListByText(stateid);
			//设置是否显示节点步奏和节点步骤的内容
			StringBuffer sbstept = new StringBuffer();
			String isshowstept = "";
			String steptype = "";
			if (steps.size()<=1){
				//设置是否显示节点步奏
				isshowstept = "none";
				StepEntity entity = (StepEntity) steps.get(0);
				sbstept.append("<input type='hidden' name='steptype' id='steptype0' value='"+entity.getId()+"' >");
			}
			else{
				isshowstept = "block";
				int i = 0;
				for(Iterator iter = steps.iterator();iter.hasNext();)
				{
					StepEntity entity = (StepEntity)iter.next();
					if (i==0){
						sbstept.append("<input type='radio' name='steptype' id='steptype"+i+"' checked style='border:0px;padding: 5px;width: 50px;margin-bottom: 5px;color: #666;background: url(../images/input_bg.gif) repeat-x top;' value='"+entity.getId()+"'/>"+entity.getText()+"&nbsp;&nbsp;&nbsp;&nbsp;") ;
					}
					else{
						sbstept.append("<input type='radio' name='steptype' id='steptype"+i+"'  style='border:0px;padding: 5px;width: 50px;margin-bottom: 5px;color: #666;background: url(../images/input_bg.gif) repeat-x top;' value='"+entity.getId()+"'/>"+entity.getText()+"&nbsp;&nbsp;&nbsp;&nbsp;");
					}
					i++;
				}
			}
			String spyj = flowtask.getSpyj()!=null?flowtask.getSpyj():"";
			getRequest().setAttribute("flowname", flowtask.getFlowName()); //设置flowname
			getRequest().setAttribute("flowtaskid", flowtask.getId());
			getRequest().setAttribute("mainid", flowtask.getMainId());
			getRequest().setAttribute("flowid", flowid);
			getRequest().setAttribute("taskinfoid", taskinfoid);
			getRequest().setAttribute("stateid", flowtaskinfo.getFlowStateId());
			getRequest().setAttribute("isshowstept", isshowstept);
			getRequest().setAttribute("steptcontent", sbstept.toString());
			
			//处理打回部分    // 回退部分。
			List<StepEntity> stepbacks = fxs.GetListByTextback(stateid);
			//设置是否显示节点步奏和节点步骤的内容
			StringBuffer sbsteptback = new StringBuffer();
			
			String isshowsteptback = "none";
			if (stepbacks.size()>0){
				if (stepbacks.size()<=1){
					//设置是否显示节点步奏
					StepEntity entity = (StepEntity) stepbacks.get(0);
					sbsteptback.append("<input type='hidden' name='steptypeback' id='steptypeback0' value='"+entity.getId()+"' >");
				}
				else{
					isshowsteptback = "block";
					int i = 0;
					for(Iterator iter = stepbacks.iterator();iter.hasNext();)
					{
						StepEntity entity = (StepEntity)iter.next();
						if (i==0){
							sbsteptback.append("<input type='radio' name='steptypeback' id='steptypeback"+i+"' checked style='border:0px;padding: 5px;width: 50px;margin-bottom: 5px;color: #666;background: url(../images/input_bg.gif) repeat-x top;' value='"+entity.getId()+"' />"+entity.getText()+"&nbsp;&nbsp;&nbsp;&nbsp;") ;
						}
						else{
							sbsteptback.append("<input type='radio' name='steptypeback' id='steptypeback"+i+"' style='border:0px;padding: 5px;width: 50px;margin-bottom: 5px;color: #666;background: url(../images/input_bg.gif) repeat-x top;' value='"+entity.getId()+"'/>"+entity.getText()+"&nbsp;&nbsp;&nbsp;&nbsp;");
						}
					}
				}
			}
			getRequest().setAttribute("isshowsteptback", isshowsteptback);
			getRequest().setAttribute("steptcontentback", sbsteptback.toString());
			
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	 
	 /*
	  * 一般更新初始化方法
	  */
	 public void initShiyanjdBase(){
		 //判断节点类型进行处理
		try {
			String flowid = this.getRequest().getParameter("flowid");
			String taskinfoid = this.getRequest().getParameter("taskinfoid");
			//设置流程类型id
			this.setFlowid(flowid);
			IFlowtypeService flowtypeService =  getFlowtypeService();
			IFlowformService flowformService =  getFlowformentityService();
			FlowTaskInfo flowtaskinfo = flowformService.getFlowTaskInfoById(taskinfoid);
			FlowTask flowtask = flowformService.getFlowTaskById(flowtaskinfo.getFlowtaskid().toString());
			Flowtype flowtype = flowtypeService.GetFlowtypeById(Long.parseLong(flowid));
			String filename = flowtype.getFilename();
			String path = PubMethod.GetFlowFilePath()+filename;
			TotalFlowXmlTool fxs = new TotalFlowXmlTool(path);
			Flowstate flowstate = fxs.GetFlowStateByID(stateid);
			//设置当前状态
			List<StepEntity> steps = fxs.GetListByText(stateid);
			//设置是否显示节点步奏和节点步骤的内容
			StringBuffer sbstept = new StringBuffer();
			String isshowstept = "";
			String spyj = flowtask.getSpyj()!=null?flowtask.getSpyj():"";
			getRequest().setAttribute("flowname", flowtask.getFlowName()); //设置flowname
			getRequest().setAttribute("flowtaskid", flowtask.getId());
			getRequest().setAttribute("mainid", flowtask.getMainId());
			getRequest().setAttribute("flowid", flowid);
			getRequest().setAttribute("taskinfoid", taskinfoid);
			getRequest().setAttribute("stateid", flowtaskinfo.getFlowStateId());
			getRequest().setAttribute("isshowstept", isshowstept);
			getRequest().setAttribute("steptcontent", sbstept.toString());

		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	 
	 /*
	  * 一般更新初始化方法
	  */
	 public void initFlowupdateBaseView(){
		 //判断节点类型进行处理
		try {
			String flowid = this.getRequest().getParameter("flowid");
			String detailid = this.getRequest().getParameter("detailid");
			//设置流程类型id
			this.setFlowid(flowid);
			IFlowtypeService flowtypeService =  getFlowtypeService();
			IFlowformService flowformService =  getFlowformentityService();
			FlowDetailed flowdetail = flowformService.getFlowDetailedById(detailid);
			FlowTask flowtask = flowformService.getFlowTaskById(flowdetail.getFlowTaskId().toString());
			Flowtype flowtype = flowtypeService.GetFlowtypeById(Long.parseLong(flowid));
			String filename = flowtype.getFilename();
			String path = PubMethod.GetFlowFilePath()+filename;
			TotalFlowXmlTool fxs = new TotalFlowXmlTool(path);
			Flowstate flowstate = fxs.GetFlowStateByID(stateid);
			//设置当前状态
			List<StepEntity> steps = fxs.GetListByText(stateid);
			//设置是否显示节点步奏和节点步骤的内容
			StringBuffer sbstept = new StringBuffer();
			String isshowstept = "";
			String steptype = "";
			if (steps.size()<=1){
				//设置是否显示节点步奏
				isshowstept = "none";
				StepEntity entity = (StepEntity) steps.get(0);
				sbstept.append("<input type='hidden' name='steptype' id='steptype0' value='"+entity.getId()+"' >");
			}
			else{
				isshowstept = "block";
				int i = 0;
				for(Iterator iter = steps.iterator();iter.hasNext();)
				{
					StepEntity entity = (StepEntity)iter.next();
					if (i==0){
						sbstept.append("<input type='radio' name='steptype' id='steptype"+i+"' checked style='border:0px;padding: 5px;width: 50px;margin-bottom: 5px;color: #666;background: url(../images/input_bg.gif) repeat-x top;' value='"+entity.getId()+"'/>"+entity.getText()+"&nbsp;&nbsp;&nbsp;&nbsp;") ;
					}
					else{
						sbstept.append("<input type='radio' name='steptype' id='steptype"+i+"'  style='border:0px;padding: 5px;width: 50px;margin-bottom: 5px;color: #666;background: url(../images/input_bg.gif) repeat-x top;' value='"+entity.getId()+"'/>"+entity.getText()+"&nbsp;&nbsp;&nbsp;&nbsp;");
					}
					i++;
				}
			}
			getRequest().setAttribute("flowname", flowtask.getFlowName()); //设置flowname
			getRequest().setAttribute("flowtaskid", flowtask.getId());
			getRequest().setAttribute("mainid", flowtask.getMainId());
			getRequest().setAttribute("flowid", flowid);
			getRequest().setAttribute("stateid", flowdetail.getStatusafterid());
			getRequest().setAttribute("isshowstept", isshowstept);
			//表示是查看页面，不显示按钮
			getRequest().setAttribute("isview", "1");
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	 
	 /*
	  * 会签初始化方法
	  */
	 public void initFlowupdateHuiqian(){
			String flowid = this.getRequest().getParameter("flowid");
			String taskinfoid = this.getRequest().getParameter("taskinfoid");
			if (taskinfoid!=null){
				//设置流程类型id
				IFlowtypeService flowtypeService =  getFlowtypeService();
				IFlowformService flowformService =  getFlowformentityService();
				try {
					FlowTaskInfo flowtaskinfo = flowformService.getFlowTaskInfoById(taskinfoid);
					String stateid = flowtaskinfo.getFlowStateId();
					FlowTask flowtask = flowformService.getFlowTaskById(flowtaskinfo.getFlowtaskid().toString());
					Flowtype flowtype = flowtypeService.GetFlowtypeById(Long.parseLong(flowid));
					String filename = flowtype.getFilename();
					String path = PubMethod.GetFlowFilePath()+filename;
					TotalFlowXmlTool fxs = new TotalFlowXmlTool(path);
					Flowstate flowstate = fxs.GetFlowStateByID(stateid);
					//设置当前状态
					List<StepEntity> steps = fxs.GetListByText(stateid);
					//设置是否显示节点步奏和节点步骤的内容
					StringBuffer sbstept = new StringBuffer();
					String isshowstept = "";
					String steptype = "";
					if (steps.size()<=1){
						//设置是否显示节点步奏
						isshowstept = "none";
						StepEntity entity = (StepEntity) steps.get(0);
						sbstept.append("<input type='hidden' name='steptype' id='steptype0' value='"+entity.getId()+"' >");
					}
					else{
						isshowstept = "block";
						int i = 0;
						for(Iterator iter = steps.iterator();iter.hasNext();)
						{
							StepEntity entity = (StepEntity)iter.next();
							if (i==0){
								sbstept.append("<input type='checkbox' id='steptype"+i+"' checked style='border:0px;padding: 5px;width: 50px;margin-bottom: 5px;color: #666;background: url(../images/input_bg.gif) repeat-x top;' value='"+entity.getId()+"' />"+entity.getText()+"&nbsp;&nbsp;&nbsp;&nbsp;") ;
							}
							else{
								sbstept.append("<input type='checkbox' id='steptype"+i+"' checked style='border:0px;padding: 5px;width: 50px;margin-bottom: 5px;color: #666;background: url(../images/input_bg.gif) repeat-x top;' value='"+entity.getId()+"'/>"+entity.getText()+"&nbsp;&nbsp;&nbsp;&nbsp;");
							}
							steptype+=entity.getId()+",";
							i++;
						}
					}
					steptype = steptype.substring(0,steptype.length()-1);
					sbstept.append("<input type='hidden' name='steptype' id='steptype' value='"+steptype+"'");
					getRequest().setAttribute("flowname", flowtask.getFlowName());
					getRequest().setAttribute("flowid", flowid);
					getRequest().setAttribute("taskinfoid", taskinfoid);
					getRequest().setAttribute("stateid", flowtaskinfo.getFlowStateId());
					getRequest().setAttribute("isshowstept", isshowstept);
					getRequest().setAttribute("steptcontent", sbstept.toString());
					
					//处理打回部分
					List<StepEntity> stepbacks = fxs.GetListByTextback(stateid);
					//设置是否显示节点步奏和节点步骤的内容
					StringBuffer sbsteptback = new StringBuffer();
					
					String isshowsteptback = "none";
					if (stepbacks.size()>0){
						if (stepbacks.size()<=1){
							//设置是否显示节点步奏
							StepEntity entity = (StepEntity) stepbacks.get(0);
							sbsteptback.append("<input type='hidden' name='steptypeback' id='steptypeback0' value='"+entity.getId()+"' >");
						}
						else{
							isshowsteptback = "block";
							int i = 0;
							for(Iterator iter = stepbacks.iterator();iter.hasNext();)
							{
								StepEntity entity = (StepEntity)iter.next();
								if (i==0){
									sbsteptback.append("<input type='radio' name='steptypeback' id='steptypeback"+i+"' checked style='border:0px;padding: 5px;width: 50px;margin-bottom: 5px;color: #666;background: url(../images/input_bg.gif) repeat-x top;' value='"+entity.getId()+"' />"+entity.getText()+"&nbsp;&nbsp;&nbsp;&nbsp;") ;
								}
								else{
									sbsteptback.append("<input type='radio' name='steptypeback' id='steptypeback"+i+"' style='border:0px;padding: 5px;width: 50px;margin-bottom: 5px;color: #666;background: url(../images/input_bg.gif) repeat-x top;' value='"+entity.getId()+"'/>"+entity.getText()+"&nbsp;&nbsp;&nbsp;&nbsp;");
								}
							}
						}
					}
					
					getRequest().setAttribute("isshowsteptback", isshowsteptback);
					getRequest().setAttribute("steptcontentback", sbsteptback.toString());
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (DaoException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else{
				initFlowupdateBaseView();
			}
	}
	 
	 /*
	  * 会签初始化方法
	  */
	 public void initFlowupdateFenzhi(){
		 String flowid = this.getRequest().getParameter("flowid");
			String taskinfoid = this.getRequest().getParameter("taskinfoid");
			//设置流程类型id
			IFlowtypeService flowtypeService =  getFlowtypeService();
			IFlowformService flowformService =  getFlowformentityService();
			try {
				FlowTaskInfo flowtaskinfo = flowformService.getFlowTaskInfoById(taskinfoid);
				String stateid = flowtaskinfo.getFlowStateId();
				FlowTask flowtask = flowformService.getFlowTaskById(flowtaskinfo.getFlowtaskid().toString());
				Flowtype flowtype = flowtypeService.GetFlowtypeById(Long.parseLong(flowid));
				String filename = flowtype.getFilename();
				String path = PubMethod.GetFlowFilePath()+filename;
				TotalFlowXmlTool fxs = new TotalFlowXmlTool(path);
				Flowstate flowstate = fxs.GetFlowStateByID(stateid);
				//设置当前状态
				List<StepEntity> steps = fxs.GetListByText(stateid);
				//设置是否显示节点步奏和节点步骤的内容
				StringBuffer sbstept = new StringBuffer();
				String isshowstept = "";
				String steptype = "";
				if (steps.size()<=1){
					//设置是否显示节点步奏
					isshowstept = "none";
					StepEntity entity = (StepEntity) steps.get(0);
					sbstept.append("<input type='hidden' name='steptype' id='steptype0' value='"+entity.getId()+"' >");
				}
				else{
					isshowstept = "block";
					int i = 0;
					for(Iterator iter = steps.iterator();iter.hasNext();)
					{
						StepEntity entity = (StepEntity)iter.next();
						if (i==0){
							sbstept.append("<input type='checkbox' class='steptype'  id='steptype"+i+"' checked style='border:0px;padding: 5px;width: 50px;margin-bottom: 5px;color: #666;background: url(../images/input_bg.gif) repeat-x top;' value='"+entity.getId()+"' />"+entity.getText()+"&nbsp;&nbsp;&nbsp;&nbsp;") ;
						}
						else{
							sbstept.append("<input type='checkbox' class='steptype'  id='steptype"+i+"' checked style='border:0px;padding: 5px;width: 50px;margin-bottom: 5px;color: #666;background: url(../images/input_bg.gif) repeat-x top;' value='"+entity.getId()+"'/>"+entity.getText()+"&nbsp;&nbsp;&nbsp;&nbsp;");
						}
						steptype+=entity.getId()+",";
						i++;
					}
				}
				steptype = steptype.substring(0,steptype.length()-1);
				sbstept.append("<input type='hidden' name='steptype' id='steptype' value='"+steptype+"'");
				getRequest().setAttribute("flowname", flowtask.getFlowName());
				getRequest().setAttribute("flowid", flowid);
				getRequest().setAttribute("taskinfoid", taskinfoid);
				getRequest().setAttribute("stateid", flowtaskinfo.getFlowStateId());
				getRequest().setAttribute("isshowstept", isshowstept);
				getRequest().setAttribute("steptcontent", sbstept.toString());
				
				//处理打回部分
				List<StepEntity> stepbacks = fxs.GetListByTextback(stateid);
				//设置是否显示节点步奏和节点步骤的内容
				StringBuffer sbsteptback = new StringBuffer();
				
				String isshowsteptback = "none";
				if (stepbacks.size()>0){
					if (stepbacks.size()<=1){
						//设置是否显示节点步奏
						StepEntity entity = (StepEntity) stepbacks.get(0);
						sbsteptback.append("<input type='hidden' name='steptypeback' id='steptypeback0' value='"+entity.getId()+"' >");
					}
					else{
						isshowsteptback = "block";
						int i = 0;
						for(Iterator iter = stepbacks.iterator();iter.hasNext();)
						{
							StepEntity entity = (StepEntity)iter.next();
							if (i==0){
								sbsteptback.append("<input type='radio' name='steptypeback' id='steptypeback"+i+"' checked style='border:0px;padding: 5px;width: 50px;margin-bottom: 5px;color: #666;background: url(../images/input_bg.gif) repeat-x top;' value='"+entity.getId()+"' />"+entity.getText()+"&nbsp;&nbsp;&nbsp;&nbsp;") ;
							}
							else{
								sbsteptback.append("<input type='radio' name='steptypeback' id='steptypeback"+i+"' style='border:0px;padding: 5px;width: 50px;margin-bottom: 5px;color: #666;background: url(../images/input_bg.gif) repeat-x top;' value='"+entity.getId()+"'/>"+entity.getText()+"&nbsp;&nbsp;&nbsp;&nbsp;");
							}
						}
					}
				}
				
				getRequest().setAttribute("isshowsteptback", isshowsteptback);
				getRequest().setAttribute("steptcontentback", sbsteptback.toString());
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (DaoException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	 

	 
	 /*
	  * 一般更新保存保存
	  */
	 public String saveUpdateBase(String tablename,String mainid){
			try{
				String userId = ServletActionContext.getRequest().getSession().getAttribute("TDM_USER_ID").toString();
				String userNm = ServletActionContext.getRequest().getSession().getAttribute("username").toString();
				String stateid =getRequest().getParameter("stateid");
				String flowid = getRequest().getParameter("flowid");
				String steptype = getRequest().getParameter("steptype");
				String flowname = getRequest().getParameter("flowname");
				String taskinfoid = getRequest().getParameter("taskinfoid");
				String spyj = getRequest().getParameter("spyj");  //审批意见
				String nextUserIds = getRequest().getParameter("nextUserIds"); 
				String nextUserNames = getRequest().getParameter("nextUserNames"); 
				IFlowtypeService flowtypeService =  getFlowtypeService();
				IFlowformService flowformService =  getFlowformentityService();
				FlowTaskInfo flowtaskinfo = flowformService.getFlowTaskInfoById(taskinfoid);
				String taskid = flowtaskinfo.getFlowtaskid().toString();
				
				Flowtype flowtype = flowtypeService.GetFlowtypeById(Long.parseLong(flowid));
				String filename = flowtype.getFilename();
				String path = PubMethod.GetFlowFilePath()+filename;
				TotalFlowXmlTool fxs = new TotalFlowXmlTool(path);
				List<StepEntity> steps = fxs.GetListByText(stateid);
				Flowstate flowstate = fxs.GetNextState(steptype);
				Flowstate beforeflowstate = fxs.GetFlowStateByID(stateid);
				FlowTask flowTask = flowformService.getFlowTaskById(taskid);
				flowTask.setAddUserId(Long.parseLong(userId));
				flowTask.setAddUserName(userNm);
				flowTask.setFlowName(flowname);
				flowTask.setFlowTypeId(Long.parseLong(flowid));
				flowTask.setState(0l);
				flowTask.setFlowStateName(flowstate.getState());
				flowTask.setFlowStateId(flowstate.getStateid());
				flowTask.setTablename(tablename);
				if (!mainid.equals("")){
					flowTask.setMainId(Long.parseLong(mainid));
				}
				if (!flowstate.getRoleIds().equals(""))
				{
					flowTask.setNextRolesId(","+flowstate.getRoleIds()+",");
					flowTask.setNextRolesName(flowstate.getRoleNames());
				}else{
					flowTask.setNextRolesId(null);
					flowTask.setNextRolesName(null);
				}
				flowTask.setNextUsersId(nextUserIds);
				flowTask.setNextUsersName(nextUserNames);
				flowTask.setNextDept(flowstate.getDept());
				flowTask.setNextDeptid(flowstate.getDeptid());
				flowformService.updateFlowTask(flowTask);
				
				flowtaskinfo.setFlowStateId(flowstate.getStateid());
				flowtaskinfo.setFlowStateName(flowstate.getState());
				flowtaskinfo.setNextDept(flowstate.getDept());
				flowtaskinfo.setNextDeptid(flowstate.getDeptid());
				if (!flowstate.getRoleIds().equals(""))
				{
					flowtaskinfo.setNextRolesId(","+flowstate.getRoleIds()+",");
				}else{
					flowtaskinfo.setNextRolesId("");
				}
				flowtaskinfo.setNextUsersId(nextUserIds);
				flowtaskinfo.setNextUsersName(nextUserNames);
				flowformService.updateFlowTaskInfo(flowtaskinfo);

				FlowDetailed flowdetail = new FlowDetailed();
				flowdetail.setFlowTaskId(Long.parseLong(taskid));
				flowdetail.setFlowTypeId(flowTask.getFlowTypeId());
				flowdetail.setOperatorperson(userNm);
				flowdetail.setOperatorpersonId(Long.parseLong(userId));
				flowdetail.setStatusbeforeid(beforeflowstate.getStateid());
				flowdetail.setStatusbefore(beforeflowstate.getState());
				flowdetail.setStatusafterid(flowstate.getStateid());
				flowdetail.setStatusafter(flowstate.getState());
				flowdetail.setDeptid(Long.parseLong(this.getDeptid()));
				flowdetail.setOperatortime(new Timestamp(System.currentTimeMillis()));
				flowdetail.setActiontype("顺序");
				if(null!=spyj && !"".equals(spyj)){
					flowdetail.setRemark(spyj);
				}
				flowformService.saveFlowDetailed(flowdetail);
				return "success";
			}catch (Exception e) {
				e.printStackTrace();
				return "error";
			}
	 }
	 
	 /*
	  * 保存试验节点方法，分支开始
	  */
	 public String saveShiyanjdBase(String tablename,String mainid){
			try{
				String userId = ServletActionContext.getRequest().getSession().getAttribute("TDM_USER_ID").toString();
				String userNm = ServletActionContext.getRequest().getSession().getAttribute("username").toString();
				String stateid =getRequest().getParameter("stateid");
				String flowid = getRequest().getParameter("flowid");
				String flowname = getRequest().getParameter("flowname");
				String taskinfoid = getRequest().getParameter("taskinfoid");
				String cbstepts = getRequest().getParameter("cbstept"); 
				String spyj = getRequest().getParameter("spyj");  //审批意见
				IFlowtypeService flowtypeService =  getFlowtypeService();
				IFlowformService flowformService =  getFlowformentityService();
				FlowTaskInfo flowtaskinfo = flowformService.getFlowTaskInfoById(taskinfoid);
				String taskid = flowtaskinfo.getFlowtaskid().toString();
				
				Flowtype flowtype = flowtypeService.GetFlowtypeById(Long.parseLong(flowid));
				String filename = flowtype.getFilename();
				String path = PubMethod.GetFlowFilePath()+filename;
				TotalFlowXmlTool fxs = new TotalFlowXmlTool(path);
				List<StepEntity> steps = fxs.GetListByText(stateid);
				String steptype = "ysjl";
				Flowstate flowstate = fxs.GetNextState(steptype);
				Flowstate beforeflowstate = fxs.GetFlowStateByID(stateid);
				FlowTask flowTask = flowformService.getFlowTaskById(taskid);
				flowTask.setAddUserId(Long.parseLong(userId));
				flowTask.setAddUserName(userNm);
				flowTask.setFlowName(flowname);
				flowTask.setFlowTypeId(Long.parseLong(flowid));
				flowTask.setState(0l);
				flowTask.setFlowStateName(flowstate.getState());
				flowTask.setFlowStateId(flowstate.getStateid());
				flowTask.setTablename(tablename);
				
				if (!mainid.equals("")){
					flowTask.setMainId(Long.parseLong(mainid));
				}
				if (!flowstate.getRoleIds().equals(""))
				{
					flowTask.setNextRolesId(","+flowstate.getRoleIds()+",");
				}else{
					flowTask.setNextRolesId("");
				}
				flowTask.setNextRolesName(flowstate.getRoleNames());
				flowTask.setNextUsersName(flowstate.getUserNames());
				flowTask.setNextDept(flowstate.getDept());
				flowTask.setNextDeptid(flowstate.getDeptid());
				flowTask.setFenzhiflowcount(1l);
				
				flowtaskinfo.setFlowStateId(flowstate.getStateid());
				flowtaskinfo.setFlowStateName(flowstate.getState());
				flowtaskinfo.setNextDept(flowstate.getDept());
				flowtaskinfo.setNextDeptid(flowstate.getDeptid());
				if (!flowstate.getRoleIds().equals(""))
				{
					flowtaskinfo.setNextRolesId(","+flowstate.getRoleIds()+",");
				}else{
					flowtaskinfo.setNextRolesId("");
				}
				flowtaskinfo.setNextUsersId(flowstate.getUserIds());
				flowtaskinfo.setNextUsersName(flowstate.getUserNames());
				flowformService.updateFlowTaskInfo(flowtaskinfo);
				
				FlowDetailed flowdetail = new FlowDetailed();
				flowdetail.setFlowTaskId(Long.parseLong(taskid));
				flowdetail.setFlowTypeId(flowTask.getFlowTypeId());
				flowdetail.setOperatorperson(userNm);
				flowdetail.setOperatorpersonId(Long.parseLong(userId));
				flowdetail.setStatusbeforeid(beforeflowstate.getStateid());
				flowdetail.setStatusbefore(beforeflowstate.getState());
				flowdetail.setStatusafterid(flowstate.getStateid());
				flowdetail.setStatusafter(flowstate.getState());
				flowdetail.setDeptid(Long.parseLong(this.getDeptid()));
				flowdetail.setOperatortime(new Timestamp(System.currentTimeMillis()));
				flowdetail.setActiontype("顺序");
				if(null!=spyj && !"".equals(spyj)){
					flowdetail.setRemark(spyj);
				}
				flowformService.saveFlowDetailed(flowdetail);
				
				if (cbstepts.indexOf("lhsqd")>=0){
					Flowstate flowstatelhsqd = fxs.GetNextState("lhsqd");
					FlowTaskInfo newflowtaskinfo = new FlowTaskInfo();
					newflowtaskinfo.setFlowtaskid(Long.parseLong(taskid));
					newflowtaskinfo.setFlowStateId("tianxielhsqd");
					newflowtaskinfo.setFlowStateName("填写理化申请单");
					newflowtaskinfo.setNextDept(flowstatelhsqd.getDept());
					newflowtaskinfo.setNextDeptid(flowstatelhsqd.getDeptid());
					if (!flowstatelhsqd.getRoleIds().equals(""))
					{
						newflowtaskinfo.setNextRolesId(","+flowstatelhsqd.getRoleIds()+",");
					}else{
						newflowtaskinfo.setNextRolesId("");
					}
					newflowtaskinfo.setNextUsersId(flowstatelhsqd.getUserIds());
					newflowtaskinfo.setNextUsersName(flowstatelhsqd.getUserNames());
					newflowtaskinfo.setState(0l);
					flowformService.saveFlowTaskInfo(newflowtaskinfo);
					
					FlowDetailed flowdetaillhsqd = new FlowDetailed();
					flowdetaillhsqd.setFlowTaskId(Long.parseLong(taskid));
					flowdetaillhsqd.setFlowTypeId(flowTask.getFlowTypeId());
					flowdetaillhsqd.setOperatorperson(userNm);
					flowdetaillhsqd.setOperatorpersonId(Long.parseLong(userId));
					flowdetaillhsqd.setStatusbeforeid(beforeflowstate.getStateid());
					flowdetaillhsqd.setStatusbefore(beforeflowstate.getState());
					flowdetaillhsqd.setStatusafterid(flowstatelhsqd.getStateid());
					flowdetaillhsqd.setStatusafter(flowstatelhsqd.getState());
					flowdetaillhsqd.setDeptid(Long.parseLong(this.getDeptid()));
					flowdetaillhsqd.setOperatortime(new Timestamp(System.currentTimeMillis()));
					flowdetaillhsqd.setActiontype("顺序");
					if(null!=spyj && !"".equals(spyj)){
						flowdetaillhsqd.setRemark(spyj);
					}
					flowformService.saveFlowDetailed(flowdetaillhsqd);
					flowTask.setFenzhiflowcount(2l);
				}
				flowformService.updateFlowTask(flowTask);
				return "success";
			}catch (Exception e) {
				e.printStackTrace();
				return "error";
			}
	 }
	 
	 
	 /**
	  * 一般退回
	  * @param isDeleteAll 如果为true则删除所有子任务，如果为false则不删除子任务
	  * @return
	  */
	 public String backUpdateBase(boolean isDeleteAll){
			try{
				String userId = ServletActionContext.getRequest().getSession().getAttribute("TDM_USER_ID").toString();
				String userNm = ServletActionContext.getRequest().getSession().getAttribute("username").toString();
				String stateid =getRequest().getParameter("stateid");
				String flowid = getRequest().getParameter("flowid");
				String steptype = getRequest().getParameter("steptypeback");
				String flowname = getRequest().getParameter("flowname");
				String taskinfoid = getRequest().getParameter("taskinfoid");
				IFlowtypeService flowtypeService =  getFlowtypeService();
				IFlowformService flowformService =  getFlowformentityService();
				FlowTaskInfo flowtaskinfo = flowformService.getFlowTaskInfoById(taskinfoid);
				String taskid = flowtaskinfo.getFlowtaskid().toString();
				
				Flowtype flowtype = flowtypeService.GetFlowtypeById(Long.parseLong(flowid));
				String filename = flowtype.getFilename();
				String path = PubMethod.GetFlowFilePath()+filename;
				TotalFlowXmlTool fxs = new TotalFlowXmlTool(path);
				List<StepEntity> steps = fxs.GetListByText(stateid);
				Flowstate flowstate = fxs.GetNextState(steptype);
				Flowstate beforeflowstate = fxs.GetFlowStateByID(stateid);
				FlowTask flowTask = flowformService.getFlowTaskById(taskid);
				flowTask.setAddUserId(Long.parseLong(userId));
				flowTask.setAddUserName(userNm);
				flowTask.setFlowName(flowname);
				flowTask.setFlowTypeId(Long.parseLong(flowid));
				flowTask.setState(0l);
				flowTask.setFlowStateName(flowstate.getState());
				flowTask.setFlowStateId(flowstate.getStateid());
				if (!flowstate.getRoleIds().equals(""))
				{
					flowTask.setNextRolesId(","+flowstate.getRoleIds()+",");
				}else{
					flowTask.setNextRolesId("");
				}
				flowTask.setNextRolesName(flowstate.getRoleNames());
				flowTask.setNextUsersName(flowstate.getUserNames());
				flowTask.setNextDept(flowstate.getDept());
				flowTask.setNextDeptid(flowstate.getDeptid());
				flowformService.saveFlowTask(flowTask);
				
				//如果为true则删除所有子任务，如果为false则不删除子任务
				if(isDeleteAll){
					flowformService.delFlowTaskInfoAll(taskid);
				}
				FlowTaskInfo flowtaskinfonew = new FlowTaskInfo();
				flowtaskinfonew.setFlowStateId(flowstate.getStateid());
				flowtaskinfonew.setFlowStateName(flowstate.getState());
				flowtaskinfonew.setNextDept(flowstate.getDept());
				flowtaskinfonew.setNextDeptid(flowstate.getDeptid());
				if (!flowstate.getRoleIds().equals(""))
				{
					flowtaskinfonew.setNextRolesId(","+flowstate.getRoleIds()+",");
				}else{
					flowtaskinfonew.setNextRolesId("");
				}
				flowtaskinfonew.setNextUsersId(flowstate.getUserIds());
				flowtaskinfonew.setNextUsersName(flowstate.getUserNames());
				flowtaskinfonew.setFlowtaskid(Long.parseLong(taskid));
				flowtaskinfonew.setState(0l);
				flowformService.saveFlowTaskInfo(flowtaskinfonew);
				
				FlowDetailed flowdetail = new FlowDetailed();
				flowdetail.setFlowTypeId(flowTask.getFlowTypeId());
				flowdetail.setOperatorperson(userNm);
				flowdetail.setOperatorpersonId(Long.parseLong(userId));
				flowdetail.setStatusbeforeid(beforeflowstate.getStateid());
				flowdetail.setStatusbefore(beforeflowstate.getState());
				flowdetail.setStatusafterid(flowstate.getStateid());
				flowdetail.setStatusafter(flowstate.getState());
				flowdetail.setDeptid(Long.parseLong(this.getDeptid()));
				flowdetail.setOperatortime(new Timestamp(System.currentTimeMillis()));
				flowdetail.setActiontype("打回");
				flowformService.saveFlowDetailed(flowdetail);
				return "success";
			}catch (Exception e) {
				e.printStackTrace();
				return "error";
			}
	 }
	 
	 /*
	  * 会签开始保存
	  */
	 public String saveUpdateHuiqianstart(String tablename,String mainid){
			try{
				String userId = ServletActionContext.getRequest().getSession().getAttribute("TDM_USER_ID").toString();
				String userNm = ServletActionContext.getRequest().getSession().getAttribute("username").toString();
				String stateid =getRequest().getParameter("stateid");
				String flowid = getRequest().getParameter("flowid");
				String steptype = getRequest().getParameter("steptype");
				String[] arraysteptype = steptype.split(",");
				String flowname = getRequest().getParameter("flowname");
				String taskinfoid = getRequest().getParameter("taskinfoid");
				IFlowtypeService flowtypeService =  getFlowtypeService();
				IFlowformService flowformService =  getFlowformentityService();
				FlowTaskInfo flowtaskinfo = flowformService.getFlowTaskInfoById(taskinfoid);
				String taskid = flowtaskinfo.getFlowtaskid().toString();
				flowformService.delFlowTaskInfo(flowtaskinfo);
				
				Flowtype flowtype = flowtypeService.GetFlowtypeById(Long.parseLong(flowid));
				String filename = flowtype.getFilename();
				String path = PubMethod.GetFlowFilePath()+filename;
				TotalFlowXmlTool fxs = new TotalFlowXmlTool(path);
				List<StepEntity> steps = fxs.GetListByText(stateid);
				Flowstate beforeflowstate = fxs.GetFlowStateByID(stateid);
				FlowTask flowTask = flowformService.getFlowTaskById(taskid);
				flowTask.setFlowName(flowname);
				flowTask.setFlowTypeId(Long.parseLong(flowid));
				flowTask.setTablename(tablename);
				if (!mainid.equals("")){
					flowTask.setMainId(Long.parseLong(mainid));
				}
				flowTask.setFenzhiflowcount((long)arraysteptype.length);
				flowformService.saveFlowTask(flowTask);

				for(int i=0;i<arraysteptype.length;i++){
					String flowtypeitem = arraysteptype[i];
					Flowstate flowstate = fxs.GetNextState(flowtypeitem);
					FlowTaskInfo flowtaskinfonew = new  FlowTaskInfo();
					flowtaskinfonew.setFlowtaskid(Long.parseLong(taskid));
					flowtaskinfonew.setFlowStateId(flowstate.getStateid());
					flowtaskinfonew.setFlowStateName(flowstate.getState());
					flowtaskinfonew.setNextDept(flowstate.getDept());
					flowtaskinfonew.setNextDeptid(flowstate.getDeptid());
					if (!flowstate.getRoleIds().equals(""))
					{
						flowtaskinfonew.setNextRolesId(","+flowstate.getRoleIds()+",");
					}else{
						flowtaskinfonew.setNextRolesId("");
					}
					flowtaskinfonew.setNextUsersId(flowstate.getUserIds());
					flowtaskinfonew.setNextUsersName(flowstate.getUserNames());
					flowtaskinfonew.setState(0l);
					flowformService.saveFlowTaskInfo(flowtaskinfonew);
					
					FlowDetailed flowdetail = new FlowDetailed();
					flowdetail.setFlowTaskId(Long.parseLong(taskid));
					flowdetail.setFlowTypeId(flowTask.getFlowTypeId());
					flowdetail.setOperatorperson(userNm);
					flowdetail.setOperatorpersonId(Long.parseLong(userId));
					flowdetail.setStatusbeforeid(beforeflowstate.getStateid());
					flowdetail.setStatusbefore(beforeflowstate.getState());
					flowdetail.setStatusafterid(flowstate.getStateid());
					flowdetail.setStatusafter(flowstate.getState());
					flowdetail.setDeptid(Long.parseLong(this.getDeptid()));
					flowdetail.setOperatortime(new Timestamp(System.currentTimeMillis()));
					flowdetail.setActiontype("顺序");
					flowformService.saveFlowDetailed(flowdetail);
				}
				return "success";
			}catch (Exception e) {
				e.printStackTrace();
				return "error";
			}
	 }
	 
	 /*
	  * 会签结束保存
	  */
	 public String saveUpdateHuiqianend(String tablename,String mainid){
			try{
				String userId = ServletActionContext.getRequest().getSession().getAttribute("TDM_USER_ID").toString();
				String userNm = ServletActionContext.getRequest().getSession().getAttribute("username").toString();
				String stateid =getRequest().getParameter("stateid");

				String flowid = getRequest().getParameter("flowid");
				String steptype = getRequest().getParameter("steptype");
				String flowname = getRequest().getParameter("flowname");
				String taskinfoid = getRequest().getParameter("taskinfoid");
				IFlowtypeService flowtypeService =  getFlowtypeService();
				IFlowformService flowformService =  getFlowformentityService();
				FlowTaskInfo flowtaskinfo = flowformService.getFlowTaskInfoById(taskinfoid);
				String taskid = flowtaskinfo.getFlowtaskid().toString();
				String nextUserIds = getRequest().getParameter("nextUserIds"); 
				String nextUserNames = getRequest().getParameter("nextUserNames");

				Flowtype flowtype = flowtypeService.GetFlowtypeById(Long.parseLong(flowid));
				String filename = flowtype.getFilename();
				String path = PubMethod.GetFlowFilePath()+filename;
				TotalFlowXmlTool fxs = new TotalFlowXmlTool(path);
				
				Flowstate flowstate = fxs.GetNextState(steptype);
				Flowstate beforeflowstate = fxs.GetFlowStateByID(stateid);
				FlowTask flowTask = flowformService.getFlowTaskById(taskid);
				flowTask.setFlowName(flowname);
				flowTask.setFlowTypeId(Long.parseLong(flowid));
				flowTask.setFlowStateName(flowstate.getState());
				flowTask.setFlowStateId(flowstate.getStateid());
				flowTask.setTablename(tablename);
				if (!mainid.equals("")){
					flowTask.setMainId(Long.parseLong(mainid));
				}
				if (!flowstate.getRoleIds().equals(""))
				{
					flowTask.setNextRolesId(","+flowstate.getRoleIds()+",");
				}else{
					flowTask.setNextRolesId("");
				}
				flowTask.setNextRolesName(flowstate.getRoleNames());
				flowTask.setNextUsersName(flowstate.getUserNames());
				flowTask.setNextDept(flowstate.getDept());
				flowTask.setNextDeptid(flowstate.getDeptid());
				flowformService.updateFlowTask(flowTask);
				//流程图中会签汇聚节点的数量
				int flowsteptcount = Integer.parseInt(flowTask.getFenzhiflowcount().toString());
				//获得数据库中已完成的汇聚节点的数量
				int databasesteptcount = flowformService.getFlowtaskinfoStatecount(taskid, flowstate.getStateid());
				if (databasesteptcount+1<flowsteptcount){
					flowtaskinfo.setFlowStateId(flowstate.getStateid());
					flowtaskinfo.setFlowStateName(flowstate.getState());
					flowtaskinfo.setNextDept(flowstate.getDept());
					flowtaskinfo.setNextDeptid(flowstate.getDeptid());
					if (!flowstate.getRoleIds().equals(""))
					{
						flowtaskinfo.setNextRolesId(","+flowstate.getRoleIds()+",");
					}else{
						flowtaskinfo.setNextRolesId("");
					}
					flowtaskinfo.setNextUsersId(nextUserIds);
					flowtaskinfo.setNextUsersName(nextUserNames);
					flowtaskinfo.setState(1l);
					flowformService.updateFlowTaskInfo(flowtaskinfo);
				}
				else{
					flowformService.delFlowTaskInfoAll(taskid);
					FlowTaskInfo flowtaskinfonew = new FlowTaskInfo();
					flowtaskinfonew.setFlowtaskid(Long.parseLong(taskid));
					flowtaskinfonew.setFlowStateId(flowstate.getStateid());
					flowtaskinfonew.setFlowStateName(flowstate.getState());
					flowtaskinfonew.setNextDept(flowstate.getDept());
					flowtaskinfonew.setNextDeptid(flowstate.getDeptid());
					if (!flowstate.getRoleIds().equals(""))
					{
						flowtaskinfonew.setNextRolesId(","+flowstate.getRoleIds()+",");
					}else{
						flowtaskinfonew.setNextRolesId("");
					}
					flowtaskinfo.setNextUsersId(nextUserIds);
					flowtaskinfo.setNextUsersName(nextUserNames);
					flowtaskinfonew.setState(0l);
					flowformService.saveFlowTaskInfo(flowtaskinfonew);
					
					flowTask.setFenzhiflowcount(0l);
					flowformService.updateFlowTask(flowTask);
				}
				
				FlowDetailed flowdetail = new FlowDetailed();
				flowdetail.setFlowTaskId(Long.parseLong(taskid));
				flowdetail.setFlowTypeId(flowTask.getFlowTypeId());
				flowdetail.setOperatorperson(userNm);
				flowdetail.setOperatorpersonId(Long.parseLong(userId));
				flowdetail.setStatusbeforeid(beforeflowstate.getStateid());
				flowdetail.setStatusbefore(beforeflowstate.getState());
				flowdetail.setStatusafterid(flowstate.getStateid());
				flowdetail.setStatusafter(flowstate.getState());
				flowdetail.setDeptid(Long.parseLong(this.getDeptid()));
				flowdetail.setOperatortime(new Timestamp(System.currentTimeMillis()));
				flowdetail.setActiontype("顺序");
				flowformService.saveFlowDetailed(flowdetail);
				return "success";
			}catch (Exception e) {
				e.printStackTrace();
				return "error";
			}
	 }
	 
	 /*
	  * 分支开始保存
	  */
	 public String saveUpdateFenzhistart(String tablename,String mainid){
			try{
				String userId = ServletActionContext.getRequest().getSession().getAttribute("TDM_USER_ID").toString();
				String userNm = ServletActionContext.getRequest().getSession().getAttribute("username").toString();
				String stateid =getRequest().getParameter("stateid");
				String flowid = getRequest().getParameter("flowid");
				String steptype = getRequest().getParameter("steptype");
				String[] arraysteptype = steptype.split(",");
				String flowname = getRequest().getParameter("flowname");
				String taskinfoid = getRequest().getParameter("taskinfoid");
				IFlowtypeService flowtypeService =  getFlowtypeService();
				IFlowformService flowformService =  getFlowformentityService();
				FlowTaskInfo flowtaskinfo = flowformService.getFlowTaskInfoById(taskinfoid);
				String taskid = flowtaskinfo.getFlowtaskid().toString();
				flowformService.delFlowTaskInfo(flowtaskinfo);
				
				Flowtype flowtype = flowtypeService.GetFlowtypeById(Long.parseLong(flowid));
				String filename = flowtype.getFilename();
				String path = PubMethod.GetFlowFilePath()+filename;
				TotalFlowXmlTool fxs = new TotalFlowXmlTool(path);
				List<StepEntity> steps = fxs.GetListByText(stateid);
				Flowstate beforeflowstate = fxs.GetFlowStateByID(stateid);
				FlowTask flowTask = flowformService.getFlowTaskById(taskid);
				flowTask.setFlowName(flowname);
				flowTask.setFlowTypeId(Long.parseLong(flowid));
				flowTask.setTablename(tablename);
				if (!mainid.equals("")){
					flowTask.setMainId(Long.parseLong(mainid));
				}
				flowTask.setFenzhiflowcount((long)arraysteptype.length);
				flowformService.saveFlowTask(flowTask);
				
				for(int i=0;i<arraysteptype.length;i++){
					String flowtypeitem = arraysteptype[i];
					Flowstate flowstate = fxs.GetNextState(flowtypeitem);
					FlowTaskInfo flowtaskinfonew = new  FlowTaskInfo();
					flowtaskinfonew.setFlowtaskid(Long.parseLong(taskid));
					flowtaskinfonew.setFlowStateId(flowstate.getStateid());
					flowtaskinfonew.setFlowStateName(flowstate.getState());
					flowtaskinfonew.setNextDept(flowstate.getDept());
					flowtaskinfonew.setNextDeptid(flowstate.getDeptid());
					if (!flowstate.getRoleIds().equals(""))
					{
						flowtaskinfonew.setNextRolesId(","+flowstate.getRoleIds()+",");
					}else{
						flowtaskinfonew.setNextRolesId("");
					}
					flowtaskinfonew.setNextUsersId(flowstate.getUserIds());
					flowtaskinfonew.setNextUsersName(flowstate.getUserNames());
					flowtaskinfonew.setState(0l);
					flowformService.saveFlowTaskInfo(flowtaskinfonew);
					
					FlowDetailed flowdetail = new FlowDetailed();
					flowdetail.setFlowTaskId(Long.parseLong(taskid));
					flowdetail.setFlowTypeId(flowTask.getFlowTypeId());
					flowdetail.setOperatorperson(userNm);
					flowdetail.setOperatorpersonId(Long.parseLong(userId));
					flowdetail.setStatusbeforeid(beforeflowstate.getStateid());
					flowdetail.setStatusbefore(beforeflowstate.getState());
					flowdetail.setStatusafterid(flowstate.getStateid());
					flowdetail.setStatusafter(flowstate.getState());
					flowdetail.setDeptid(Long.parseLong(this.getDeptid()));
					flowdetail.setOperatortime(new Timestamp(System.currentTimeMillis()));
					flowdetail.setActiontype("顺序");
					flowformService.saveFlowDetailed(flowdetail);
				}
				return "success";
			}catch (Exception e) {
				e.printStackTrace();
				return "error";
			}
	 }
	 
	 /*
	  * 分支结束保存
	  */
	 public String saveUpdateFenzhiend(String tablename,String mainid){
			try{
				String userId = ServletActionContext.getRequest().getSession().getAttribute("TDM_USER_ID").toString();
				String userNm = ServletActionContext.getRequest().getSession().getAttribute("username").toString();
				String stateid =getRequest().getParameter("stateid");
				String flowid = getRequest().getParameter("flowid");
				String steptype = getRequest().getParameter("steptype");
				String flowname = getRequest().getParameter("flowname");
				String taskinfoid = getRequest().getParameter("taskinfoid");
				String nextUserIds = getRequest().getParameter("nextUserIds"); 
				String nextUserNames = getRequest().getParameter("nextUserNames"); 
				IFlowtypeService flowtypeService =  getFlowtypeService();
				IFlowformService flowformService =  getFlowformentityService();
				FlowTaskInfo flowtaskinfo = flowformService.getFlowTaskInfoById(taskinfoid);
				String taskid = flowtaskinfo.getFlowtaskid().toString();
				
				Flowtype flowtype = flowtypeService.GetFlowtypeById(Long.parseLong(flowid));
				String filename = flowtype.getFilename();
				String path = PubMethod.GetFlowFilePath()+filename;
				TotalFlowXmlTool fxs = new TotalFlowXmlTool(path);
				
				Flowstate flowstate = fxs.GetNextState(steptype);
				Flowstate beforeflowstate = fxs.GetFlowStateByID(stateid);
				FlowTask flowTask = flowformService.getFlowTaskById(taskid);
				flowTask.setFlowName(flowname);
				flowTask.setFlowTypeId(Long.parseLong(flowid));
				flowTask.setFlowStateName(flowstate.getState());
				flowTask.setFlowStateId(flowstate.getStateid());
				flowTask.setTablename(tablename);
				if (!mainid.equals("")){
					flowTask.setMainId(Long.parseLong(mainid));
				}
				if (!flowstate.getRoleIds().equals(""))
				{
					flowTask.setNextRolesId(","+flowstate.getRoleIds()+",");
				}else{
					flowTask.setNextRolesId("");
				}
				flowTask.setNextRolesName(flowstate.getRoleNames());
				flowTask.setNextUsersId(nextUserIds);
				flowTask.setNextUsersName(nextUserNames);
				flowTask.setNextDept(flowstate.getDept());
				flowTask.setNextDeptid(flowstate.getDeptid());
				flowformService.updateFlowTask(flowTask);
				//流程图中会签汇聚节点的数量
				int flowsteptcount = Integer.parseInt(flowTask.getFenzhiflowcount().toString());
				//获得数据库中已完成的汇聚节点的数量
				int databasesteptcount = flowformService.getFlowtaskinfoStatecount(taskid, flowstate.getStateid());
				if (databasesteptcount+1<flowsteptcount){
					flowtaskinfo.setFlowStateId(flowstate.getStateid());
					flowtaskinfo.setFlowStateName(flowstate.getState());
					flowtaskinfo.setNextDept(flowstate.getDept());
					flowtaskinfo.setNextDeptid(flowstate.getDeptid());
					if (!flowstate.getRoleIds().equals(""))
					{
						flowtaskinfo.setNextRolesId(","+flowstate.getRoleIds()+",");
					}else{
						flowtaskinfo.setNextRolesId("");
					}
					flowtaskinfo.setNextUsersId(nextUserIds);
					flowtaskinfo.setNextUsersName(nextUserNames);
					flowtaskinfo.setState(1l);
					flowformService.updateFlowTaskInfo(flowtaskinfo);
				}
				else{
					flowformService.delFlowTaskInfoAll(taskid);
					FlowTaskInfo flowtaskinfonew = new FlowTaskInfo();
					flowtaskinfonew.setFlowtaskid(Long.parseLong(taskid));
					flowtaskinfonew.setFlowStateId(flowstate.getStateid());
					flowtaskinfonew.setFlowStateName(flowstate.getState());
					flowtaskinfonew.setNextDept(flowstate.getDept());
					flowtaskinfonew.setNextDeptid(flowstate.getDeptid());
					if (!flowstate.getRoleIds().equals(""))
					{
						flowtaskinfonew.setNextRolesId(","+flowstate.getRoleIds()+",");
					}else{
						flowtaskinfonew.setNextRolesId("");
					}
					flowtaskinfonew.setNextUsersId(nextUserIds);
					flowtaskinfonew.setNextUsersName(nextUserNames);
					flowtaskinfonew.setState(0l);
					flowformService.saveFlowTaskInfo(flowtaskinfonew);
					
					flowTask.setFenzhiflowcount(0l);
					flowformService.updateFlowTask(flowTask);
				}
				
				FlowDetailed flowdetail = new FlowDetailed();
				flowdetail.setFlowTaskId(Long.parseLong(taskid));
				flowdetail.setFlowTypeId(flowTask.getFlowTypeId());
				flowdetail.setOperatorperson(userNm);
				flowdetail.setOperatorpersonId(Long.parseLong(userId));
				flowdetail.setStatusbeforeid(beforeflowstate.getStateid());
				flowdetail.setStatusbefore(beforeflowstate.getState());
				flowdetail.setStatusafterid(flowstate.getStateid());
				flowdetail.setStatusafter(flowstate.getState());
				flowdetail.setDeptid(Long.parseLong(this.getDeptid()));
				flowdetail.setOperatortime(new Timestamp(System.currentTimeMillis()));
				flowdetail.setActiontype("顺序");
				flowformService.saveFlowDetailed(flowdetail);
				return "success";
			}catch (Exception e) {
				e.printStackTrace();
				return "error";
			}
	 }
	 
	 
	 
	 public String getNextSteptRolesBase(){
		 try{
			String flowid = getRequest().getParameter("flowid");
		 	IFlowtypeService flowtypeService =  getFlowtypeService();
			Flowtype flowtype = flowtypeService.GetFlowtypeById(Long.parseLong(flowid));
			String filename = flowtype.getFilename();
			String path = PubMethod.GetFlowFilePath()+filename;
			String steptype = getRequest().getParameter("steptype"); // 表单控件
			TotalFlowXmlTool fxs = new TotalFlowXmlTool(path);
			Flowstate flowstate = fxs.GetNextState(steptype);
			String roles = "";
			if (flowstate.getRoleIds().equals("")){
				return "";
			}
			else{
				roles = flowstate.getRoleIds();
//				if (roles.length()>0){
//					roles = roles.substring(0,roles.length()-1);
//				}
//				if (roles.length()>0){
//					roles = roles.substring(1,roles.length());
//				}
			}
			return roles;
		 }catch (Exception e) {
			e.printStackTrace();
			return "error";
		 }
	}
		
	 
	 
	 /**
	 * 根据参数获取session中的信息
	 * @param att
	 * @return
	 */
	public Object getSessionAttValue(String att){
		return getHttpSession().getAttribute(att);
	}
	public void setContentType(String type){
		getResponse().setContentType(type);
	}
	public void setContentType(){
		setContentType("text/json; charset=utf-8");
	}
	public void setContentTypeHtml(){
		setContentType("text/html; charset=utf-8");
	}
	protected final HttpSession getHttpSession() {
		return getRequest().getSession();
	}
 
	public HttpServletRequest getRequest(){
		return ServletActionContext.getRequest();
	}
	
	public HttpServletResponse getResponse(){
		return ServletActionContext.getResponse();
		
	}
	
	public PageContext getPageContext()
	{
		return ServletActionContext.getPageContext();
	}

	public HttpSession getSession(){
		return getRequest().getSession();
	}
	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getSidx() {
		return sidx;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public String getSord() {
		return sord;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}
	
	public long getStart() {
		return start;
	}
	public void setStart(long start) {
		this.start = start;
	}
	public long getLimit() {
		return limit;
	}
	public void setLimit(long limit) {
		this.limit = limit;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getDir() {
		return dir;
	}
	public void setDir(String dir) {
		this.dir = dir;
	}
	public String getStateid() {
		return stateid;
	}
	public void setStateid(String stateid) {
		this.stateid = stateid;
	}
	public String getFlowid() {
		return flowid;
	}
	public void setFlowid(String flowid) {
		this.flowid = flowid;
	}
	public String getTaskid() {
		return taskid;
	}
	public void setTaskid(String taskid) {
		this.taskid = taskid;
	}
	public String getMainid() {
		return mainid;
	}
	public void setMainid(String mainid) {
		this.mainid = mainid;
	}
	public String getSteptcontent() {
		return steptcontent;
	}
	public void setSteptcontent(String steptcontent) {
		this.steptcontent = steptcontent;
	}
	public String getIsshowstept() {
		return isshowstept;
	}
	public void setIsshowstept(String isshowstept) {
		this.isshowstept = isshowstept;
	}
 
}
